"""
  # File       : checkPoint.py
  # Time       : 2021/12/13
  # Author     : XGT
  # version    : V1.0.0
  # Description: 实现关键字驱动和数据驱动联动,连接框架unittest
  # Note       : CheckPoint.checkAssert_raise()用于抛出异常，主要用于报告
                 因为return 与 raise 不能前后共用,故而提取出该独立方法
                 TestRunner可以在源码中自动修改报告名称和测试人等信息,
                 可实现自动发邮件功能send_email()
                 每个表格做一条用例，无细细区分
"""

import unittest
from Log.logger import Log
from Base.Base import BaseOfWeb
from ddt import ddt, data
from Control import FileControl
from Control import ExcelControl
from common import checkPoint
from unittestreport import TestRunner
from common import LogUtil as logu
import cProfile

test_data = FileControl.get_excel_file()

@ddt
class Test(unittest.TestCase):
    @data(*test_data)
    @logu.logExceptionHandler(logu.exceptionHandler)
    def test_login(self,test_data):
        Log.info(test_data)
        excel_path1 = test_data[len(test_data) - 1]
        for i in range(0,len(test_data)-1):
            if test_data[i]['event'] == 'open_browers':
                wk = BaseOfWeb(str(test_data[i]['txt']))
                Log.info('现在执行关键字:{0}，结果:{1}'.format(test_data[i]['event'],'正常'))
            elif 'assert' in test_data[i]['event']:
                Res = getattr(wk,test_data[i]['event'])(**test_data[i])
                if checkPoint.CheckPoint().checkAssertEqual(Res,test_data[i]['except']):
                    staut = 'OK'
                    ExcelControl.write_Test_Result(excel_path1,Res,i,staut)
                    Log.info('现在执行关键字:{0}，结果:{1}'.format(test_data[i]['event'], '断言成功'))
                    Log.info('断言成功！实际结果为:{0}={1}'.format(Res,test_data[i]['except'])+'\n')
                else:
                    staut = 'Failed'
                    ExcelControl.write_Test_Result(excel_path1,Res,i, staut)
                    Log.error('现在执行关键字:{0}，结果:{1}!,{2}{3}错误位置位于{4}文件的第{5}行，出现错误。{6}{7}操作描述为:{8},实际结果为:{9}!={10}{11}'
                              .format(test_data[i]['event'],'断言失败','\n','                                                                ',excel_path1,str(i + 2),'\n','                                                                ',
                                      test_data[i]['Des'],Res,test_data[i]['except'],'\n'))
                    checkPoint.checkAssert_raise(Res, test_data['except'])
            elif 'click' in test_data[i]['event']:
                # time.sleep(1)
                getattr(wk, test_data[i]['event'])(**test_data[i])
                Log.info('现在执行关键字:{0}，操作描述:{1}，执行结果:{2}'.format(test_data[i]['event'],test_data[i]['Des'],'正常'))
            else:
                getattr(wk, test_data[i]['event'])(**test_data[i])
                Log.info('现在执行关键字:{0}，操作描述为:{1}，执行结果:{2}'.format(test_data[i]['event'], test_data[i]['Des'],'正常'))
            # time.sleep(1)


def suite():
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    suite.addTest(loader.loadTestsFromTestCase(Test))
    return suite


if __name__ == '__main__':

    br = TestRunner(suite())
    br.run()
    cProfile.run('suite')

    # br.report(filename='testReport.html',Test = "sdasassd",description="测试报告",log_path='./report/')